//
//  WBMainViewController.swift
//  tangbinWeibo
//
//  Created by 唐斌 on 2016/12/25.
//  Copyright © 2016年 唐斌. All rights reserved.
//

import UIKit


/// 主控制器
class WBMainViewController: UITabBarController
{
    
    lazy var composeButton: UIButton = UIButton(type: .custom)

    override func viewDidLoad()
    {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        setupChildControllers()
        
        setupComposeButton()
        
    }
    
    // MARK: -监听方法
    /// 撰写微博
    // FIXME: 没有实现
    
    func clickedCompose(sender: UIButton)
    {
        print(#function)
    }

}


// extension 类似于OC中得分类，在Swift中还可以用来切分代码块
// 可以把相近功能的函数，放在一个extension中
// 便于代码维护
// 注意：和OC的分类一样，extension中不能定义属性

// MARK: - 设置界面
extension WBMainViewController
{
    //  'clickedCompose' is inaccessible due to 'private' protection level
    
    func setupComposeButton()
    {
        composeButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        composeButton.backgroundColor = UIColor.orange
        composeButton.setTitle("+", for: .normal);
//        CGRect frame = tabBar.bounds
        tabBar.addSubview(composeButton)
        composeButton.addTarget(self, action: #selector(clickedCompose), for: .touchUpInside)
        let count = CGFloat(childViewControllers.count)
        let w = tabBar.bounds.width / count - 1
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
//        composeButton.frame = CGRect(x: 2 * w, y: 4, width: 40, height: 40)
        
    }
    
    /// 设置所有的子控制器
    func setupChildControllers()
    {
        let array = [
            ["clsName": "WBHomeViewController", "title": "首页", "imageName": "mainpage"],
            ["clsName": "WBMessageViewController", "title": "消息", "imageName": "guide"],
            ["clsName":"UIViewController"],
            ["clsName": "WBDisconverViewController", "title": "发现", "imageName": "travelers"],
            ["clsName": "WBProfileViewController", "title": "我", "imageName": "self"],
        ]
        
        var arrayM = [UIViewController]()
        for dict in array
        {
            arrayM.append(controller(dict: dict))
        }
        viewControllers = arrayM
    }
    
    
    /// 使用字典创建一个子控制器
    ///
    /// - Parameter dict: 信息字典[clsName, title, imageName]
    /// - Returns: 子控制器
    private func controller(dict: [String: String]) -> UIViewController
    {
        guard let clsName = dict["clsName"],
                let title = dict["title"],
                let imageName = dict["imageName"],
                let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type else {
            
            return UIViewController();
        }
        
        // let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
        
        
        /*let clsName = "4007-反射机制.ViewController"
        // AnyClass?
        let cls = NSClassFromString(clsName) as? UIViewController.Type
        let vc = cls?.init()
    */
        // 创建控制器
        let vc = cls.init()
        vc.title = title;
        vc.tabBarItem.image = UIImage(named: "tabbar_"+imageName+"_nor")
        vc.tabBarItem.selectedImage = UIImage(named: "tabar_"+imageName+"_sel")?.withRenderingMode(.alwaysOriginal)
        
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blue, NSFontAttributeName: UIFont.systemFont(ofSize: 12)], for: .highlighted)
        
        
        
        
        let nav = WBNavigationController(rootViewController: vc)
        
        return nav
    }
}











































